import request from '@/utils/request';

// 获取作品列表（分页）
export async function listWorks(page: number = 1, pageSize: number = 9): Promise<any> {
  try {
    const response = await request.get('/works/listWorks', {
      params: { page, page_size: pageSize }
    });
    return response.data;
  } catch (error) {
    console.error('获取作品列表失败:', error);
    return { data: [], total: 0 };
  }
}

// 根据分类ID获取作品列表（分页）
export async function getWorksByCategory(categoryId: number, page: number = 1, pageSize: number = 9): Promise<any> {
  try {
    const response = await request.get(`/works/category/${categoryId}`, {
      params: { page, page_size: pageSize }
    });
    return response.data;
  } catch (error) {
    console.error('获取分类作品列表失败:', error);
    return { data: [], total: 0 };
  }
}

// 根据父分类ID获取作品列表（分页）
export async function getWorksByParentCategory(parentId: number, page: number = 1, pageSize: number = 9): Promise<any> {
  try {
    const response = await request.get(`/works/parent-category/${parentId}`, {
      params: { page, page_size: pageSize }
    });
    return response.data;
  } catch (error) {
    console.error('获取父分类作品列表失败:', error);
    return { data: [], total: 0 };
  }
}

// 获取单个作品
export async function getWork(id: number): Promise<any | null> {
  try {
    const response = await request.get(`/works/getWork/${id}`);
    return response.data.data || null;
  } catch (error) {
    console.error('获取作品失败:', error);
    return null;
  }
}

// 根据关键词搜索作品（分页）
export async function searchWorks(keyword: string, page: number = 1, pageSize: number = 9): Promise<any> {
  try {
    const response = await request.get(`/works/search`, {
      params: {keyword:keyword, page, page_size: pageSize }
    });
    return response.data;
  } catch (error) {
    console.error('搜索作品失败:', error);
    return { data: [], total: 0 };
  }
}